Skip to content

feat(storage): add Object Contexts samples and system tests#2200

Open
salilg-eng wants to merge 14 commits intoGoogleCloudPlatform:mainfrom
salilg-eng:feat/sample-object-contexts
Open

feat(storage): add Object Contexts samples and system tests#2200
salilg-eng wants to merge 14 commits intoGoogleCloudPlatform:mainfrom
salilg-eng:feat/sample-object-contexts

Conversation

@salilg-eng
Copy link
Copy Markdown

@salilg-eng salilg-eng commented Apr 20, 2026

  • Add set_object_contexts.php to demonstrate upload contexts.
  • Add get_object_contexts.php to show retrieval of structured metadata.
  • Add list_object_contexts.js to demonstrate server-side filtering.
  • Implement comprehensive system tests in ObjectTest.php covering presence, absence, and existence filter operators.
  • Ensure samples use the correct 'contexts' field with 'custom' map structure.

@salilg-eng salilg-eng requested review from a team as code owners April 20, 2026 13:32
@snippet-bot
Copy link
Copy Markdown

snippet-bot Bot commented Apr 20, 2026

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples. labels Apr 20, 2026
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 20, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces three new PHP samples for managing Cloud Storage object contexts: get_object_contexts, list_object_contexts, and set_object_contexts. It also includes a comprehensive test case in ObjectsTest.php to verify these functionalities. Feedback highlights several issues in the test implementation, including the use of an uninitialized project ID variable which results in invalid bucket names, a resource leak due to buckets not being deleted after tests, and redundant variable assignments. Additionally, it is recommended to use null coalescing operators in the samples for better robustness and to include missing filter operator test cases mentioned in the PR description.

Comment thread storage/test/ObjectsTest.php Outdated
Comment on lines +47 to +104
$objectContextsBucket = self::$storage->createBucket(
sprintf('%s-test-bucket-%s', self::$projectId, time())
);
$bucketName = $objectContextsBucket->name();
$objectName = 'object-contexts-'.uniqid();
$object = $objectContextsBucket->upload('test', [
'name' => $objectName,
]);
$objectName = $object->info()['name'];

// Set Object Contexts
$setOutput = $this->runFunctionSnippet('set_object_contexts', [
$bucketName,
$objectName,
]);

$this->assertStringContainsString("Contexts for object $objectName were updated", $setOutput);

// Get Object Contexts
$getOutput = $this->runFunctionSnippet('get_object_contexts', [
$bucketName,
$objectName
]);

$this->assertStringContainsString('Key: department, Value: finance', $getOutput);
$this->assertStringContainsString('Key: priority, Value: high', $getOutput);

// List Object Contexts using filter
$listOutput = $this->runFunctionSnippet('list_object_contexts', [
$bucketName
]);
$this->assertStringContainsString("Found object: $objectName", $listOutput);

// Clear all contexts on the object.
$object->update([
'contexts' => [
'custom' => null,
],
]);
$object->reload();

$clearedGetOutput = $this->runFunctionSnippet('get_object_contexts', [
$bucketName,
$objectName
]);

$this->assertStringNotContainsString('Key: department, Value: finance', $clearedGetOutput);
$this->assertStringNotContainsString('Key: priority, Value: high', $clearedGetOutput);

$clearedInfo = $object->info();
$this->assertTrue(empty($clearedInfo['contexts']['custom'] ?? []));

// List again with filter after clearing contexts.
$clearedListOutput = $this->runFunctionSnippet('list_object_contexts', [
$bucketName
]);
$this->assertEmpty(trim($clearedListOutput));
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The bucket created for this test is never deleted, leading to resource leaks in the test project. Ensure the bucket is deleted at the end of the test, or consider using the shared bucket self::$bucketName if isolation is not strictly required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes both object and bucket deleted

Comment thread storage/test/ObjectsTest.php
Comment thread storage/test/ObjectsTest.php Outdated
Comment thread storage/src/get_object_contexts.php Outdated
Copy link
Copy Markdown

@nidhiii-27 nidhiii-27 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a small comment, rest all LGTM

Comment thread storage/src/list_object_contexts.php Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants